package com.corpcredit.webapp.action;

import java.io.Serializable;

import com.corpcredit.model.WorkQueue;
import com.corpcredit.webapp.action.BasePage;
import com.corpcredit.service.GenericManager;

public class WorkQueueForm extends BasePage implements Serializable {
    private GenericManager<WorkQueue, Long> workQueueManager;
    private WorkQueue workQueue = new WorkQueue();
    private Long id;

    public void setWorkQueueManager(GenericManager<WorkQueue, Long> workQueueManager) {
        this.workQueueManager = workQueueManager;
    }

    public WorkQueue getWorkQueue() {
        return workQueue;
    }

    public void setWorkQueue(WorkQueue workQueue) {
        this.workQueue = workQueue;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String delete() {
        workQueueManager.remove(workQueue.getId());
        addMessage("workQueue.deleted");

        return "list";
    }

    public String edit() {
        // Comparison to zero (vs. null) is required with MyFaces 1.2.2, not with previous versions
        if (id != null && id != 0) {
            workQueue = workQueueManager.get(id);
        } else {
            workQueue = new WorkQueue();
        }

        return "edit";
    }

    public String save() {
        boolean isNew = (workQueue.getId() == null || workQueue.getId() == 0);
        workQueueManager.save(workQueue);

        String key = (isNew) ? "workQueue.added" : "workQueue.updated";
        addMessage(key);

        if (isNew) {
            return "list";
        } else {
            return "edit";
        }
    }
} 